package com.arithmeticEveryDay.blueBridge;

import java.util.Arrays;
import java.util.Scanner;
/*
错误答案orz，等一手题解吧
 */

public class Main3132 {

    int n;
    int[] arr;
    int minNum;
    int maxNum;
    public Main3132(int n, int[] arr, int minNum, int maxNum) {
        this.n = n;
        this.arr = arr;
        this.minNum = minNum;
        this.maxNum = maxNum;
    }

    private long getRightPrimeNum(long mid) {
        long leftPrimNum = 0;
        long rightPrimNum = 0;
        for (long i = mid; i > minNum; i--) {
            boolean isPrime = true;
            for (long j = 2; j*j <= i; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                leftPrimNum = i;
                break;
            }
        }
        for (long i = mid; i < maxNum; i++) {
            boolean isPrime = true;
            for (long j = 2; j*j <= i; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                rightPrimNum = i;
                break;
            }
        }
        double leftGap = mid - leftPrimNum;
        double rightGap = rightPrimNum - mid;
        return leftGap < rightGap ? leftPrimNum : rightPrimNum;
    }

    public long getMinCount() {
        long avg = getAvg(arr);
        long rightPrimeNum = getRightPrimeNum(avg);
        long count = 0;
        for (int i = 0; i < n; i++) {
            count += Math.abs(rightPrimeNum-arr[i]);
            System.out.println(count);
        }
        return count;
    }

    private long getAvg(int[] arr) {
        long sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum / arr.length;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] arr = new int[n];

        arr[0] = scan.nextInt();
        int minNum = arr[0];
        int maxNum = arr[0];
        for (int i = 1; i < n; i++) {
            arr[i] = scan.nextInt();
            if (arr[i] < minNum) {
                minNum = arr[i];
            }
            if (arr[i] > maxNum) {
                maxNum = arr[i];
            }
        }
        Main3132 m = new Main3132(n, arr, minNum, maxNum);
        System.out.println(m.getMinCount());
    }
}
